package com.tianya.portal.blog.service.impl;

import com.tianya.common.text.Convert;
import com.tianya.common.utils.EncryptionUtils;
import com.tianya.common.web.exception.BusinessException;
import com.tianya.common.web.utils.StringUtils;
import com.tianya.plugtool.redis.RedisCache;
import com.tianya.portal.blog.constant.BlogConstant;
import com.tianya.portal.blog.domain.BlogNoteBook;
import com.tianya.portal.blog.domain.BlogStatistics;
import com.tianya.portal.blog.domain.vo.BlogNoteBookVo;
import com.tianya.portal.blog.mapper.BlogNoteBookMapper;
import com.tianya.portal.blog.mapper.BlogNoteTopicMapper;
import com.tianya.portal.blog.service.IBlogNoteBookService;
import com.tianya.portal.system.service.ISysDictDataService;
import com.tianya.portal.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 博客笔记Service业务层处理
 *
 * @author tianya
 * @date 2023-04-30
 */
@Service
public class BlogNoteBookServiceImpl implements IBlogNoteBookService {
    @Autowired
    private BlogNoteBookMapper blogNoteBookMapper;

    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private BlogNoteTopicMapper blogNoteTopicMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询博客笔记
     *
     * @param id 博客笔记主键
     * @return 博客笔记
     */
    @Override
    public BlogNoteBook selectBlogNoteBookById(Long id, String password) {
        BlogNoteBook noteBook = blogNoteBookMapper.selectBlogNoteBookById(id);
        //非公开要效验密码
        if (noteBook.getIsPublic().equals("0")) {
            if (StringUtils.isEmpty(password)) {
                throw new BusinessException(500, "该文档需要密码，才能查看！");
            }
            if (!EncryptionUtils.encrypt(password).equals(noteBook.getPassword())) {
                throw new BusinessException(500, "密码错误！");
            }
        }
        if (StringUtils.isNotEmpty(noteBook.getCategory())) {
            noteBook.setCategorys(Convert.toStrArray(noteBook.getCategory()));
        }
        if (StringUtils.isNotEmpty(noteBook.getTag())) {
            noteBook.setTags(Convert.toStrArray(noteBook.getTag()));
        }
        // 判断是不是公开，不公开则加密
        if (StringUtils.isNotEmpty(noteBook.getIsPublic()) && noteBook.getIsPublic().equals("0")) {
            if (StringUtils.isNotEmpty(noteBook.getContent())) {
                noteBook.setContent(EncryptionUtils.decrypt(noteBook.getContent()));
            }
        }
        blogNoteBookMapper.updateViewNumber(id);
        return noteBook;
    }

    /**
     * 获取笔记统计
     *
     * @return
     */
    @Override
    public BlogStatistics getBlogStatistics() {
        BlogStatistics blogStatistics = new BlogStatistics();
        if (redisCache.hasKey(BlogConstant.BlogStatistics_key)) {
            Object data = redisCache.getCacheObject(BlogConstant.BlogStatistics_key);
            blogStatistics = (BlogStatistics) data;
        } else {
            int category = dictTypeService.selectDictDataByType("blog_note_category").size();
            int tag = dictTypeService.selectDictDataByType("blog_note_tag").size();
            int books = blogNoteBookMapper.selectBlogNoteBookCount();
            int topic = blogNoteTopicMapper.selectBlogNoteTopicCount();
            blogStatistics.setNoteBookNumber(books);
            blogStatistics.setNoteTagNumber(tag);
            blogStatistics.setNoteCategoryNumber(category);
            blogStatistics.setNoteTopicNumber(topic);
            redisCache.setCacheObject(BlogConstant.BlogStatistics_key, blogStatistics, 7200, TimeUnit.SECONDS);
        }


        return blogStatistics;
    }

    /**
     * 查询博客笔记列表
     *
     * @param blogNoteBook 博客笔记
     * @return 博客笔记
     */
    @Override
    public List<BlogNoteBookVo> selectBlogNoteBookList(BlogNoteBook blogNoteBook) {
        List<BlogNoteBookVo> blogNoteBooks = blogNoteBookMapper.selectBlogNoteBookList(blogNoteBook);
        for (BlogNoteBookVo noteBook : blogNoteBooks) {
            if (StringUtils.isNotEmpty(noteBook.getCategory())) {
                noteBook.setCategorys(Convert.toStrArray(noteBook.getCategory()));
            }
            if (StringUtils.isNotEmpty(noteBook.getTag())) {
                noteBook.setTags(Convert.toStrArray(noteBook.getTag()));
            }
        }
        return blogNoteBooks;
    }

    /**
     * 新增博客笔记
     *
     * @param blogNoteBook 博客笔记
     * @return 结果
     */
    @Override
    public int insertBlogNoteBook(BlogNoteBook blogNoteBook) {

        return blogNoteBookMapper.insertBlogNoteBook(blogNoteBook);
    }

    /**
     * 修改博客笔记
     *
     * @param blogNoteBook 博客笔记
     * @return 结果
     */
    @Override
    public int updateBlogNoteBook(BlogNoteBook blogNoteBook) {
        return blogNoteBookMapper.updateBlogNoteBook(blogNoteBook);
    }

    /**
     * 批量删除博客笔记
     *
     * @param ids 需要删除的博客笔记主键
     * @return 结果
     */
    @Override
    public int deleteBlogNoteBookByIds(Long[] ids) {
        return blogNoteBookMapper.deleteBlogNoteBookByIds(ids);
    }

    /**
     * 删除博客笔记信息
     *
     * @param id 博客笔记主键
     * @return 结果
     */
    @Override
    public int deleteBlogNoteBookById(Long id) {
        return blogNoteBookMapper.deleteBlogNoteBookById(id);
    }
}
